﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace TProjectTool
{
    class Program
    {
        

        private static Dictionary<string, string> dict = new Dictionary<string, string>();
        static void Main(string[] args)
        {
            FileCopy(args); 
        }
        private static void FileCopy(string[] args)
        {
            if (args != null && args.Length > 0)
            {
                int i = 0, j;
                while (i < args.Length)
                {
                    if (args[i].StartsWith("/"))
                    {
                        string val = "";
                        if (i<args.Length-1 && !args[i + 1].StartsWith("/"))
                        {
                            val = args[i + 1];
                            j = i + 1;
                        }
                        else
                        {
                            j = i;
                        }

                        if (!dict.ContainsKey(args[i]))
                        {
                            dict.Add(args[i], val);
                        }
                        else
                        {
                            Console.WriteLine("参数重复定义");
                            return;
                        }
                        i = j + 1;
                    }
                }
            }

            if (dict.ContainsKey("/?"))
            {
                Console.WriteLine("/s 指定源目录，可选项，不加默认cmd当前目录");
                Console.WriteLine("/g 指定目标目录，必项项，不存在则建立目录");
                Console.WriteLine("/t 文件修改的最小时间，必项项");
                Console.WriteLine("/tend  文件修改的最大时间，可选项，不填写则是当前时间");
                Console.WriteLine("/f 过滤文件名,可选，默认所有文件，支持通配符！*，?");
                return;
            }

            DateTime dt, dtend;
            string goalpath = "", sourcepath;
            if (!dict.ContainsKey("/t"))
            {
                Console.WriteLine("缺少时间参数!");
                return;
            }
            else
            {
                string sdate = dict["/t"];
                if (!DateTime.TryParse(sdate, out dt))
                {
                    Console.WriteLine("时间参数格式不正确!");
                    return;
                }
            }

            if (!dict.ContainsKey("/tend"))
            {
                dtend = DateTime.Now;
            }
            else
            {
                string sdate = dict["/tend"];
                if (!DateTime.TryParse(sdate, out dtend))
                {
                    dtend = DateTime.Now;
                }
            }

            if (!dict.ContainsKey("/g"))
            {
                Console.WriteLine("缺少目标路径参数!");
                return;
            }
            else
            {
                try
                {
                    goalpath = dict["/g"];
                    if (goalpath.IndexOf(":") == -1)
                    {                        //相对路径
                        goalpath = Directory.GetCurrentDirectory() + goalpath;
                    }
                    if (!System.IO.Directory.Exists(goalpath))
                    {
                        System.IO.Directory.CreateDirectory(goalpath);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("错误的目标路径!" + goalpath);
                    return;
                }
            }
            if (!dict.ContainsKey("/s"))
            {
                sourcepath = Directory.GetCurrentDirectory();
            }
            else
            {
                sourcepath = dict["/s"];
                if (sourcepath.IndexOf(":") == -1)
                {                        //相对路径
                    sourcepath = Directory.GetCurrentDirectory() + sourcepath;
                }
                if (!System.IO.Directory.Exists(sourcepath))
                {
                    Console.WriteLine("错误的源路径!" + sourcepath);
                    return;
                }
            }

            string pattern;
            //格式过滤
            if (!dict.ContainsKey("/f") || string.IsNullOrEmpty(dict["/f"]))
            {
                pattern = "*.*";
            }
            else
            {
                pattern = dict["/f"];
            }

            var dir = new DirectoryInfo(sourcepath);
            var files = dir.GetFiles(pattern, System.IO.SearchOption.AllDirectories);
            List<FileInfo> lf = new List<FileInfo>();
            foreach (var file in files)
            {
                if (file.LastWriteTime >= dt && file.LastWriteTime <= dtend)
                {
                    lf.Add(file);
                }
            }
            string sourceFile = "", goalFile, tempDir;
            int slen = sourcepath.Length;
            int idx;
            try
            {
                foreach (var file in lf)
                {
                    sourceFile = file.FullName;
                    goalFile = goalpath + file.FullName.Substring(slen);
                    idx = goalFile.LastIndexOf("\\");
                    tempDir = goalFile.Substring(0, idx);
                    if (!Directory.Exists(tempDir))
                    {
                        Directory.CreateDirectory(tempDir);
                    }
                    File.Copy(sourceFile, goalFile, true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("拷贝文件出错:" + sourceFile);
                Console.WriteLine(ex.Message);
                return;
            }
            Console.WriteLine("拷贝完成!\r\n源路径:{1}\r\n{0}个文件被拷贝\r\n最后拷贝的文件{2}", lf.Count, sourcepath, sourceFile);


        }
    }
}
